﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Globalization;

namespace JinYuan.ControlLib.UCHelper
{
    //
    // 摘要:
    //     对属性进行排序
    public class PropertyOrderConverter : TypeConverter
    {
        //
        // 摘要:
        //     当该属性为展开属性选型时，属性编辑器删除该属性的描述
        //
        // 参数:
        //   context:
        //
        //   culture:
        //
        //   value:
        //
        //   destinationType:
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return string.Empty;
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value, attributes);
            ArrayList arrayList = new ArrayList();
            foreach (PropertyDescriptor item in properties)
            {
                Attribute attribute = item.Attributes[typeof(PropertyOrderAttribute)];
                arrayList.Add(new PropertyOrderPair(item.Name, (attribute == null) ? 0f : ((PropertyOrderAttribute)attribute).Order));
            }

            arrayList.Sort();
            ArrayList arrayList2 = new ArrayList();
            foreach (PropertyOrderPair item2 in arrayList)
            {
                arrayList2.Add(item2.CategoryName);
            }

            return properties.Sort((string[])arrayList2.ToArray(typeof(string)));
        }
    }
}
